package com.mc.read.adminapi.manager;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mc.read.common.emun.ServiceCodeEnum;
import com.mc.read.common.exception.ServiceException;
import com.mc.read.common.util.TreeUtils;
import com.mc.read.dao.mapper.UseropinionMapper;
import com.mc.read.dao.mapper.sys.SyslogMapper;
import com.mc.read.dao.mapper.sys.SyspermissionMapper;
import com.mc.read.dao.models.Useropinion;
import com.mc.read.dao.models.sys.Syslog;
import com.mc.read.dao.models.sys.Sysuser;
import com.mc.read.dao.vo.LogPageVo;
import com.mc.read.dao.vo.PageResposeVo;
import com.mc.read.dao.vo.StatusPageVo;
import com.mc.read.dao.vo.TreeNode;
import com.mc.read.vo.request.BaseRequest;
import com.mc.read.vo.respose.BaseRespose;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author liuhao
 * @date 2019/6/13 11:43
 */
@Component
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
public class SystemManager {
    @Autowired
    SyslogMapper syslogMapper;
    @Autowired
    UseropinionMapper useropinionMapper;
    @Autowired
    CacheManager cacheManager;
    @Autowired
    SyspermissionMapper syspermissionMapper;

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 查询日志列表 selectLogList
     **/
    public BaseRespose selectLogList(BaseRequest baseRequest) {
        BaseRespose baseRespose = new BaseRespose();
        LogPageVo logPageVo = null;
        try {
            logPageVo = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), LogPageVo.class);
        } catch (Exception e) {
            throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL);
        }
        PageHelper.startPage(logPageVo.getPagenum(), logPageVo.getPagesize());
        PageHelper.orderBy("LogId desc");
        List<Syslog> syslogList = syslogMapper.selectLogList(logPageVo);
        PageInfo<Syslog> pageInfo = new PageInfo<>(syslogList);
        PageResposeVo pageResposeVo = new PageResposeVo(pageInfo.getTotal(), pageInfo.getList());
        baseRespose.setReturnString(pageResposeVo);
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 查询意见反馈列表 selectOpinionList
     **/
    public BaseRespose selectOpinionList(BaseRequest baseRequest) {
        BaseRespose baseRespose = new BaseRespose();

        StatusPageVo statusPageVo = null;
        try {
            statusPageVo = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), StatusPageVo.class);
        } catch (Exception e) {
            throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL);
        }
        PageHelper.startPage(statusPageVo.getPagenum(), statusPageVo.getPagesize());
        PageHelper.orderBy("createDate desc");
        List<Useropinion> useropinionList = useropinionMapper.selectOpinionList(statusPageVo);
        PageInfo<Useropinion> pageInfo = new PageInfo<>(useropinionList);
        PageResposeVo pageResposeVo = new PageResposeVo(pageInfo.getTotal(), pageInfo.getList());
        baseRespose.setReturnString(pageResposeVo);
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 处理意见反馈 dealOpinion
     **/
    public BaseRespose dealOpinion(BaseRequest baseRequest) {
        BaseRespose baseRespose = new BaseRespose();
        Useropinion useropinion = null;
        try {
            useropinion = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), Useropinion.class);
        } catch (Exception e) {
            throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL);
        }
        if (ObjectUtils.isEmpty(useropinion.getUseropinionid()) || ObjectUtils.isEmpty(useropinion.getDealexplain())) {
            throw new ServiceException(203020, "请求信息不完整", JSON.toJSONString(baseRequest));
        }
        Useropinion useropinion1 = useropinionMapper.selectByPrimaryKey(useropinion.getUseropinionid());
        if (ObjectUtils.isEmpty(useropinion1)) {
            throw new ServiceException(203020, "未找到该记录", JSON.toJSONString(baseRequest));
        }
        if (useropinion1.getIsdeal() == 2) {
            throw new ServiceException(203020, "只能处理未处理的记录", JSON.toJSONString(baseRequest));
        }
        Sysuser sysuser = cacheManager.getSysUser(baseRequest.getUserId());
        System.out.println(JSON.toJSONString(sysuser));
        useropinion.setIsdeal(2);
        useropinion.setDealdate(new Date());
        useropinion.setDealid(sysuser.getUserid().toString());
        useropinion.setDealname(sysuser.getName());
        useropinionMapper.updateByPrimaryKeySelective(useropinion);
        baseRespose.setReturnString(" ");
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 菜单栏渲染 selectMenuList
     **/
    public BaseRespose selectMenuList(BaseRequest baseRequest) {
        BaseRespose baseRespose = new BaseRespose();

        Sysuser sysuser = cacheManager.getSysUser(baseRequest.getUserId());
        if (sysuser.getUsertype() == 1) {
            List<TreeNode> treeNodeList = syspermissionMapper.selectMenuListByUser(baseRequest.getUserId());
            treeNodeList = TreeUtils.listToTree(treeNodeList);
            baseRespose.setReturnString(treeNodeList);
        } else {
            List<TreeNode> treeNodeList = syspermissionMapper.selectMenuList();
            treeNodeList = TreeUtils.listToTree(treeNodeList);
            baseRespose.setReturnString(treeNodeList);
        }
        return baseRespose;
    }


    public static void main(String[] args) {
        List<String> strings = new ArrayList<>();
        strings.add("64654");
        strings.add("asdasd");
        strings.add("xxxx");
        strings.add("646123123asd54");
        String str = JSON.toJSONString(strings);
        System.out.println(str);
        List<String> strings1 = JSON.parseArray(str, String.class);
        for (String str11 : strings1
                ) {
            System.out.println(str11);
        }
    }


}
